package com.qys.livesMall.shop.service.impl;

import com.qys.livesMall.common.constants.Constants;
import com.qys.livesMall.common.message.MessageServer;
import com.qys.livesMall.finance.enums.AccountBusinessTypeEnum;
import com.qys.livesMall.finance.model.dto.FinancePayDTO;
import com.qys.livesMall.finance.service.FinanceBusinessService;
import com.qys.livesMall.shop.constants.PayMethod;
import com.qys.livesMall.shop.modal.entity.H2ShopBase;
import com.qys.livesMall.shop.modal.entity.ShopOrderSku;
import com.qys.livesMall.shop.service.EarnestMoneyDeductionService;
import com.qys.livesMall.shop.service.H2ShopBaseService;
import com.qys.livesMall.shop.service.ShopOrderSkuService;
import com.qys.livesMall.system.service.IH2SysConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class EarnestMoneyDeductionServiceImpl implements EarnestMoneyDeductionService {

    @Autowired
    private H2ShopBaseService shopBaseService;

    @Autowired
    private FinanceBusinessService financeBusinessService;
    @Autowired
    private ShopOrderSkuService shopOrderSkuService;

    @Autowired
    private MessageServer messageServer;

    @Autowired
    private IH2SysConfigService sysConfigService;
    @Override
    public void deduction() {
        log.info("保证金抵扣货款开始....");
        int outTime=1;
        String time=sysConfigService.getConfigKey("earnest_money_deduction_out_time");
        if (Strings.isBlank(time)){
            outTime=24;
        }else {
            outTime=Integer.parseInt(time);
        }
        List<ShopOrderSku> skus = shopOrderSkuService.getByNoPayCost(LocalDateTime.now().plusHours(outTime));
        if (CollectionUtils.isEmpty(skus)){
            return;
        }
        Map<Long,List<ShopOrderSku>> shopOrderMap= skus.stream().collect(Collectors.groupingBy(ShopOrderSku::getShopId));
        Map<Long, H2ShopBase> shops=shopBaseService.listByIds(shopOrderMap.keySet().stream().toList()).stream().collect(Collectors.toMap(H2ShopBase::getId,v->v));
        for(Long shopId: shopOrderMap.keySet()){
            H2ShopBase shop=shops.get(shopId);
            List<ShopOrderSku> orderSkus=shopOrderMap.get(shopId);
            //抵扣
            for (ShopOrderSku orderSku:orderSkus){
                BigDecimal cost=orderSku.getCostPrice().multiply(new BigDecimal(orderSku.getNum()));
                FinancePayDTO payDTO = new FinancePayDTO();
                payDTO.setUserId(shop.getUserId());
                payDTO.setShopId(shop.getId());
                payDTO.setBusinessNo(orderSku.getSkuOrderId());
                payDTO.setAmountIncurred(cost);
                payDTO.setBusinessType(AccountBusinessTypeEnum.EARNEST_MONEY_DEDUCTION_GOODS_PAY);
                financeBusinessService.pay(payDTO);
                if (!payDTO.getSuccess()){
                    log.info(messageServer.getMsg(payDTO.getMessage()));
                    continue;
                }
                orderSku.setCostPay(Constants.YesNoInt.YES);
                orderSku.setCostPayType(PayMethod.CostPayType.EARNEST_MONEY);
                shopOrderSkuService.updateById(orderSku);
                log.info("订单:"+orderSku.getSkuOrderId()+"保证金抵扣货款完成");
            }
        }
        log.info("保证金抵扣货款结束....");
    }


}
